const mysqlDB = require('./config/mysqlDB.config')
const Aritle = mysqlDB.import('./modules/aritle')
const Tag = mysqlDB.import('./modules/tag')
const AritleToTag = mysqlDB.import('./modules/aritleToTag')

// 使用多对多你可以基于 through 关系查询并选择特定属性，比如：
const getAritle = async () => {
  const list = await Aritle.findAll({
    raw: false,
    include: {
      model: Tag,
      as: 'tags',
      through: {
        model: AritleToTag,
        attributes: []
      }
    }
  })

  if (list) {
    for (let i = 0; i < list.length; i++) {
      console.log(JSON.parse(JSON.stringify(list[i])))
    }
  }
}

getAritle()

const getTag = async () => {
  const list = await Tag.findAll({
    raw: false,
    include: [
      {
        model: Aritle,
        as: 'aritles',
        // 通过 through 可以操作关联表的相关，
        // 比如控制显示哪些属性等等，或者按条件查询等等
        // https://itbilu.com/nodejs/npm/41qaV3czb.html
        through: {
          attributes: []
        }
        // attributes: ['id', 'name'],
        // exclude: [mysqlDB.col['aritleToTag']]
      }
    ]
  })
  if (list) {
    for (let i = 0; i < list.length; i++) {
      console.log(JSON.parse(JSON.stringify(list[i])))
    }
  }
}

getTag()
